package elijah.base.utils;

import android.widget.Toast;

import elijah.base.AppBase;
import es.dmoral.toasty.Toasty;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;

public class Toasts {
    private static Toast toast;

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
        }
    }

    public static void showToast(String msg) {
        if (toast != null) toast.cancel();
        toast = Toasty.info(AppBase.get().getAppContext(), msg);
        toast.show();
    }

    public static void showWarningToast(String msg) {
        if (toast != null) toast.cancel();
        toast = Toasty.warning(AppBase.get().getAppContext(), msg);
        toast.show();
    }

    public static void showErrorToast(String msg) {
        if (toast != null) toast.cancel();
        toast = Toasty.error(AppBase.get().getAppContext(), msg);
        toast.show();

    }

    public static void showSuccessToast(String msg) {
        if (toast != null) toast.cancel();
        toast = Toasty.success(AppBase.get().getAppContext(), msg);
        toast.show();
    }

    public static void showToastAsync(String msg) {
        CommonUtils.postStringToMainThread(msg, s -> {
            if (toast != null) toast.cancel();
            toast = Toasty.info(AppBase.get().getAppContext(), s);
            toast.show();
        });

    }

    public static void showWarningToastAsync(String msg) {
        CommonUtils.postStringToMainThread(msg, s -> {
            if (toast != null) toast.cancel();
            toast = Toasty.warning(AppBase.get().getAppContext(), s);
            toast.show();
        });

    }

    public static void showErrorToastAsync(String msg) {
        CommonUtils.postStringToMainThread(msg, s -> {
            if (toast != null) toast.cancel();
            toast = Toasty.error(AppBase.get().getAppContext(), s);
            toast.show();
        });
    }

    public static void showSuccessToastAsync(String msg) {
        CommonUtils.postStringToMainThread(msg, s -> {
            if (toast != null) toast.cancel();
            toast = Toasty.success(AppBase.get().getAppContext(), s);
            toast.show();
        });

    }
}
